<?
require_once('config.php');
$current_page = getValue('page','int','GET',1);
$alb_id       = getValue('alb_id','int','GET',0);
$action       = getValue('action','str','GET','');
$arr_albums = xinh_albums::getListAlbum();
$limit_page   = 20;
$start        = ($current_page -1) * $limit_page;
$limit        = $start . ',' . $limit_page;


if($action == 'search' && $alb_id != 0){
   $arr_images = xinh_images::getListImageByAlbum($alb_id,$limit);
   $total_images = xinh_images::getTotalImageAlbum($alb_id);
}else{
   $arr_images = xinh_images::getListImage($limit);
   $total_images = xinh_images::getTotalImage();
}
$pages = ceil($total_images/$limit_page);
/*----------------- Xử lý phân trang --------------------*/

$count_image = count($arr_images); 



?>
<?=$css_global?>
<?=$js_global?>
<div class="row-fluid">
   <div class="head">
      <div class="isw-grid"></div>
      <h1>Danh sách ảnh</h1>
      <div class="reload_tab" title="Reload"></div>                              
      <div class="clear"></div>
   </div>                                
   <div class="block-fluid table-sorting">
      <div id="tSortable_wrapper" class="dataTables_wrapper" >
         <div class="dataTables_filter" id="tSortable_filter">
               <div class="head-table">
                  <ul class="title-table pull-left">
                     <li>Tổng số: <?=$total_images?></li>
                     <li>Trang số: <?=$current_page?></li>
                     <li>Mỗi trang: <?=$limit_page?></li>
                  </ul>     
                  <ul class="list-field-search pull-right">
                     <form action="" method="GET">
                        <li>
                           <select name="alb_id" id="alb_id">
                              <option value="0">Chọn album</option>
                              <?php 
                                 foreach($arr_albums as $value){?>
                                    <option <?if($value['id'] == $alb_id ) echo 'selected' ?> value="<?=$value['id']?>"><?=$value['name']?> </option>
                                 <?}
                              ?>
                           </select>
                        </li>
                        <li>
                           <input type="hidden" value="search" name="action" />
                           <input type="submit" class="btn btn-success" value="Tìm kiếm"/>
                        </li>
                     </form>
                  </ul>
                  
               </div>
         </div>
         <table cellpadding="0" cellspacing="0" width="100%" class="table dataTable table-bordered" id="tSortable"  style="width: 100%;">
            <thead>
              <tr>
                  <th class="sorting_disabled selectallrow active"  rowspan="1" colspan="1" style="width: 23px;">Tất cả</th>
                  <th width="5%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">ID</th>
                  <th width="15%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Ngày</th>
                  <th width="20%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Ảnh</th>
                  <th width="5%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Active</th>
                  <th width="5%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Hot</th>
                  <th width="10%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Đại diện</th>
                  <th width="10%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">View</th>
                  <th width="10%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Like</th>
                  <th width="20%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Album</th>
                  
               </tr>
            </thead>
              <?
              if($count_image > 0){
                  foreach($arr_images as $value){
                  ?>
                     <tr class="even" data-id=<?=$value['id']?>>
                        <td class="font12 sorting_1">
                           <div class="checker check-to-delete">
                              <span>
                                 <input type="checkbox" class="checkbox-row" name="row_to_delete[]" value="<?=$value['id']?>" style="opacity: 0;"/>
                              </span>
                           </div>
                        </td>
                        <td class="font12"><?=$value['id']?></td>
                        <td class="font12"><?=date('H:m:s - d/m/Y',$value['date'])?></td>
                        <td class="font12 vid-name"><img width="60px" class="img-person" src="<?=$value['link']?>" /></td>       
                        <td class="font12 sorting_1" onclick="activeImage(this)">
                           <div class="checker">
                              <span active="<?=$value['active']?>" class="<?if($value['active'] == 1) echo 'checked';?>">
                                 <input type="checkbox" class="active-row" data-id=<?=$value['id']?>  value="<?=$value['active']?>"/>
                              </span>
                           </div>
                        </td> 
                        <td class="font12 sorting_1" onclick="checkHotImage(this)">
                           <div class="checker">
                              <span active="<?=$value['hot']?>" class="<?if($value['hot'] == 1) echo 'checked';?>">
                                 <input type="checkbox" class="hot-row" data-id=<?=$value['id']?>  value="<?=$value['hot']?>"/>
                              </span>
                           </div>
                        </td>
                        <td class="font12 sorting_1" onclick="checkIsAvatar(this)">
                           <div class="checker">
                              <span class="<?if($value['is_avatar'] == 1) echo 'checked';?>">
                                 <input type="checkbox" class="active-row" alb-id=<?=$value['alb_id']?>  data-id=<?=$value['id']?>  value="<?=$value['is_avatar']?>" <?if($value['is_avatar'] == 1) echo 'checked';?>  style="opacity: 0;"/>
                              </span>
                           </div>
                        </td> 
                        
                        <td class="font12"><?=$value['view']?></td>
                        <td class="font12"><?=$value['ilike']?></td>
                        <td class="font12"><?=$value['name']?></td>
                                             
                    </tr>
                  <?}?>
              <?}?>
          </table>
         </div>
      <div class="clear"></div>
      <div class="mult_submit pull-left">
         <span>Với những dòng đã chọn: </span>  <span class="delete-row mult-delete">Xóa</span> 
      </div>
      <div class="paging pull-right">
         <?php $pagination = pagination($current_page,$pages,'list_images.php?page=%d&action='.$action.'&alb_id='.$alb_id) ?>
         <ul>
         <?php foreach ($pagination as $link): ?>
           <li><?php echo $link ?></li>
         <?php endforeach ?>
         </ul>
      </div>
   </div>
</div>
<script>
$(function(){
   $('.mult-delete').click(function(){
      var matches = [];
      var string_cat_id = '';
      $(".checkbox-row:checked").each(function() {
          matches.push(this.value);
          string_cat_id = matches.join(',');
      });
      data = {'id':string_cat_id};
      var updateHTML = function(data){
         if(data != ''){
            alert(data);
            window.location.reload();
         }
      }
      var beforeSend = function(){
         
      }
      if(string_cat_id != ''){
         var conf = confirm("Xóa những dòng đã chọn ?");
         if(conf)
            handleAjax('ajax_delete_images.php','POST',data,updateHTML,beforeSend);   
      }else{
         alert("Chưa có dòng nào được chọn");
      }
      
   });
});
</script>